<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/commons/global.jsp"%>

<table id="tagList" style="height: 100%; width: 100%;">

</table>

<div id="tagListToolBars" style="height: auto">
	<a href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-remove',plain:true"
		onclick="tagList_remove()">删除</a>| <a href="javascript:void(0)"
		class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true"
		onclick="tagList_append()">添加</a>|<a href="javascript:void(0)"
		class="easyui-linkbutton"
		data-options="iconCls:'icon-edit',plain:true" onclick="tagList_edit()">编辑</a>
	| <a href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-undo',plain:true" onclick="tagList_undo()">取消</a>|
	<a href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-save',plain:true"
		onclick="tagList_accept()">保存</a>|<a href="javascript:void(0)"
		class="easyui-linkbutton"
		data-options="iconCls:'fa fa-cog',plain:true"
		onclick="tagRelationSourcePage_config()">关联资源配置</a>|<input
		id="tagName" style="width: 200px;" />
</div>
<script type="text/javascript">
	var tagQueryParams = {
		like_name : ''
	};
	function tagListDatagridReload() {
		try {
			tagQueryParams.like_name = tagName.textbox('getValue');

			tagListDatagrid.datagrid('reload');
		} catch (e) {
		}
	}
</script>
<script type="text/javascript">
	var tagName = undefined;

	var tagSelectIndex = undefined;
	var tagSelectRow = undefined;
	var tagEditIndex = undefined;
	var tagEditRow = undefined;
	var tagListDatagrid = undefined;
	$(function() {

		tagName = $('#tagName').textbox({
			buttonText : '名称搜索',
			buttonIcon : 'icon-search',
			buttonAlign : 'right'
		});

		tagName.textbox('button').click(function() {
			// 			if (!tagName.textbox('getValue')) {
			// 				return;
			// 			}
			tagListDatagridReload();
		});

		tagListDatagrid = $('#tagList').datagrid({
			url : '${baseUrl}/tag/list2',
			pagination : true,
			sortName : 'createTime',
			sortOrder : 'desc',
			multiSort : true,
			//remoteSort : false,
			// 			rownumbers : true,

			queryParams : tagQueryParams,
			singleSelect : true,
			idField : 'id',
			striped : true,
			pageSize : 20,
			fitColumns : true,
			toolbar : '#tagListToolBars',
			onLoadSuccess : function() {
				tagSelectIndex = undefined;
				tagSelectRow = undefined;
				tagEditIndex = undefined;
				tagEditRow = undefined;
			},
			onBeforeSelect : function(index, row) {
				if (tagEditIndex != undefined) {
					return false;
				} else {
					tagSelectIndex = index;
					tagSelectRow = row;
				}
			},
			onBeforeEdit : function(index, row) {
				if (tagEditIndex == undefined) {
					tagEditIndex = index;
					tagEditRow = row;
				}
			},
			onEndEdit : function(index, row, changes) {
			},
			// 			nowrap : false,
			columns : [ [ {
				field : 'id',
				title : 'ID',
				align : 'center',
				width : 60
			}, {
				field : 'text',
				title : '名称',
				width : 249,
				editor : {
					type : 'text',
					options : {
						required : true
					}
				}
			}, {
				field : 'countSource',
				title : '资源总数',
				width : 249,
				formatter : function(value, row, index) {
					return value + " 个";
				}
			}, {
				field : 'createTime',
				title : '创建时间',
				width : 100,
				align : 'left',
				sortable : true
			}, {
				field : 'createUser',
				title : '创建者',
				width : 60,
				align : 'center',
				formatter : function(value, row, index) {
					if (row.createUser) {
						return row.createUser.nickname;
					} else {
						return value;
					}
				}
			} ] ],
			onDblClickRow : function(index, row) {
				$.ajax({
					data : {
						id : row.id
					},
					async : false,
					url : baseUrl + '/tag/info.html',
					success : function(html) {
						$('body').append(html);
					}
				});

			}
		});

		tagListDatagrid.datagrid('getPager').pagination(
				{
					buttons : [ {
						iconCls : 'fa fa-2x fa-location-arrow',
						handler : function() {
							tagListDatagrid.datagrid('gotoPage',
									tagListDatagrid.datagrid('getPager').find(
											'.pagination-num').val());
						}
					} ]
				});

	});

	function tagList_append() {
		$.ajax({
			url : baseUrl + '/tag/add.html',
			success : function(html) {
				$('body').append(html);
			}
		});
	}
	function tagList_edit() {

		var tagSelect = tagListDatagrid.datagrid('getSelected');
		if (tagEditIndex != undefined && tagEditIndex != tagSelectIndex) {
			$.messager.alert('提示', '请完成已开启编辑行', 'warning');
			return;
		}

		if (tagSelect) {
			if (tagSelectIndex == undefined) {
				tagSelectIndex = tagListDatagrid.datagrid("getRowIndex",
						tagSelect);
			}

			tagListDatagrid.datagrid('beginEdit', tagSelectIndex);
			tagSelectRow = tagSelect;
		}
	}

	function tagList_undo() {
		if (tagSelectIndex != undefined) {
			tagListDatagrid.datagrid('rejectChanges');
			tagSelectIndex = undefined;
			tagSelectRow = undefined;
			tagEditIndex = undefined;
			tagEditRow = undefined;
		}
	}

	function tagList_accept() {
		if (tagSelectIndex != undefined) {
			if (tagListDatagrid.datagrid('validateRow', tagSelectIndex)) {

				//验证名称
				var _edTitle = tagListDatagrid.datagrid('getEditor', {
					index : tagSelectIndex,
					field : 'text'
				});

				var _Title = $(_edTitle.target).val();
				if (!_Title || _Title.trim() == '') {
					$.messager.alert('提示', '名称不能为空', 'warning');
					return;
				}

				var _Data = {
					id : tagSelectRow.id,
					name : _Title
				};

				$
						.ajax({
							data : _Data,
							async : false,
							type : 'POST',
							url : baseUrl + '/tag/update',
							success : function(data) {
								if (data.responseCode == '000') {
									tagListDatagrid.datagrid('endEdit',
											tagSelectIndex);

									tagListDatagrid.datagrid('acceptChanges');

									tagSelectIndex = undefined;
									tagSelectRow = undefined;
									tagEditIndex = undefined;
									tagEditRow = undefined;

									$.messager.alert('提示', '修改成功', 'info');
								} else {
									$.messager.alert('提示',
											data.responseMessage, 'error');
								}
							}
						});

			}
		}
	}

	function tagList_tagListPage() {
		var tagSelect = tagListDatagrid.datagrid('getSelected');
		if (tagSelect) {
			$.ajax({
				data : {
					itemId : tagSelect.id,
					itemType : 1
				},
				async : false,
				url : baseUrl + '/tag/set.html',
				success : function(html) {
					$('body').append(html);
				}
			});
		}
	}

	function tagList_tagListPage() {

		var tagSelect = tagListDatagrid.datagrid('getSelected');
		if (tagSelect) {
			$.ajax({
				data : {
					itemId : tagSelect.id,
					itemType : 1
				},
				async : false,
				url : baseUrl + '/tag/set.html',
				success : function(html) {
					$('body').append(html);
				}
			});
		}
	}

	function tagList_remove() {
		var tagSelect = tagListDatagrid.datagrid('getSelected');
		if (tagSelect) {
			$.messager.confirm('确认', '您确认要删除么吗？', function(r) {
				if (r) {
					tagList_remove2(tagSelect, false);
				}
			});
		}
	}

	function tagList_remove2(tagSelect, force) {
		$.ajax({
			data : {
				ids : [ tagSelect.id ],
				force : force
			},
			type : "post",
			async : false,
			url : baseUrl + '/tag/delete',
			success : function(data) {
				if (data.responseCode == '000') {
					$.messager.alert('提示', "删除成功!", 'info');

					tagListDatagrid.datagrid('deleteRow', tagListDatagrid
							.datagrid('getRowIndex', tagSelect));

				} else if (data.responseCode == '025') {
					$.messager.confirm('确认强制删除',
							'<span style="color:red;">强制删除会解除所有关联资源关系</span>',
							function(fr) {
								if (fr) {
									tagList_remove2(tagSelect, true);
								}
							});
				} else {
					$.messager.alert('提示', data.responseMessage, 'error');
				}
			}
		});
	}

	function tagRelationSourcePage_config() {
		var tagSelect = tagListDatagrid.datagrid('getSelected');
		if (tagSelect) {
			$.ajax({
				data : {
					id : tagSelect.id
				},
				async : false,
				url : baseUrl + '/tag/relation-config.html',
				success : function(html) {
					$('body').append(html);
				}
			});
		}
	}
</script>
